package com.v1.video.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.activity.PaikeVideoDetailActivity2;
import com.v1.video.activity.ThemeDetailActivity;
import com.v1.video.activity.VideoPlayerVRActivity;
import com.v1.video.activity.WebViewActivity3;
import com.v1.video.db.dao.VideoTB;
import com.v1.video.domain.FriendDynamicsEntity.FriendDynamics;
import com.v1.video.domain.ScenarioVideoInfo;
import com.v1.video.domain.SetHead;

import java.util.ArrayList;
import java.util.List;

/**
 * 好友动态适配器
 * 
 * @author wangfang 2016.4.14
 */
public class FriendDynamicsAdapter extends BaseAdapter {

	private static final String TYPE_PRAISE = "1";
	private static final String TYPE_SHARE = "2";
	private static final String TYPE_PLAY = "3";
	private static final String DESCRIBE = "了这个视频";
	private LayoutInflater mLayoutInflater;
	private Context mContext;
	private List<FriendDynamics> mLstData;

	public FriendDynamicsAdapter(Context context) {
		this.mContext = context;
		this.mLayoutInflater = LayoutInflater.from(context);
		this.mLstData = new ArrayList<FriendDynamics>();
	}

	public List<FriendDynamics> getLstData() {
		return mLstData;
	}

	public void setLstData(List<FriendDynamics> lstData) {
		if (lstData != null && lstData.size() > 0) {
			this.mLstData = lstData;
			this.notifyDataSetChanged();
		}
	}

	public void setAddLstData(List<FriendDynamics> lstData) {
		if (lstData != null && lstData.size() > 0) {
			this.mLstData.addAll(lstData);
			this.notifyDataSetChanged();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this.mLstData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return this.mLstData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Holder holder = null;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.activity_frienddynamics_item, null);
			holder = new Holder();
			holder.userimg = (ImageView) convertView
					.findViewById(R.id.iv_userimage);
			holder.ratingimg = (ImageView) convertView
					.findViewById(R.id.iv_user_rating);
			holder.name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.describe = (TextView) convertView
					.findViewById(R.id.tv_describe);
			holder.image = (ImageView) convertView.findViewById(R.id.iv_image);
			holder.title = (TextView) convertView.findViewById(R.id.tv_title);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		final FriendDynamics info = mLstData.get(position);
		if (!TextUtils.isEmpty(info.getPic())) {
			SetHead setHead = new SetHead();
			setHead.setHead(info.getPic(), "", holder.userimg);
		} else {
			holder.userimg.setImageResource(R.drawable.headimg);
		}
		if (info.getIsauth() == 1) {
			holder.ratingimg.setVisibility(View.VISIBLE);
		} else {
			holder.ratingimg.setVisibility(View.GONE);
		}
		if (!TextUtils.isEmpty(info.getNickname())) {
			holder.name.setText(info.getNickname());
		} else {
			holder.name.setText("");
		}
		if (!TextUtils.isEmpty(info.getAddtime())) {
			holder.time.setText(info.getAddtime());
		} else {
			holder.time.setText("");
		}
		if (!TextUtils.isEmpty(info.getType())) {
			if (TYPE_PRAISE.equals(info.getType())) {
				holder.describe.setText("赞" + DESCRIBE);
			} else if (TYPE_SHARE.equals(info.getType())) {
				holder.describe.setText("分享" + DESCRIBE);
			} else if (TYPE_PLAY.equals(info.getType())) {
				holder.describe.setText("播放" + DESCRIBE);
			} else {
				holder.describe.setText("");
			}
		} else {
			holder.describe.setText("");
		}
		if (info.getVideo() != null) {
			if (!TextUtils.isEmpty(info.getVideo().getTitle())) {
				holder.title.setText(info.getVideo().getTitle());
			} else {
				holder.title.setText("");
			}
			if (!TextUtils.isEmpty(info.getVideo().getPic())) {
				ImageLoader.getInstance().displayImage(
						info.getVideo().getPic(), holder.image,
						Constant.VIDEO_10_16_OPTION, null);
			} else {
				holder.image
						.setImageResource(R.drawable.common_place_holder_bg_16_10);
			}
		} else {
			holder.title.setText("");
			holder.image
					.setImageResource(R.drawable.common_place_holder_bg_16_10);
		}
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (info.getVideo() != null) {
					transferActivity(info.getVideo());
				}
			}
		});
		return convertView;
	}
	
	private void transferActivity(VideoTB video) {
		if (!TextUtils.isEmpty(video.getAt())) {
			if (Constant.VIDEO_TYPE_4.equals(video.getAt())) {
				transferWebView(video.getTitle(), video.getVideoUrl());
			} else if (Constant.VIDEO_TYPE_6.equals(video.getAt())) {
				if (video.getVideoUrl() != null
						&& video.getVideoUrl().length() >= 6) {
					transferVR(video.getVideoUrl());
				} else {
					transferVideoDetail(video);
				}
			} else if (Constant.VIDEO_TYPE_7.equals(video.getAt())) {
				transferThemeDetail(video.getTitle(), video.getTid(),
						video.getPic());
			} else {
				transferVideoDetail(video);
			}
		} else {
			transferVideoDetail(video);
		}
	}
	
	/**
	 * 跳转视频详情
	 * 
	 * @param vid
	 */
	private void transferVideoDetail(VideoTB video) {
		ScenarioVideoInfo videoInfo = new ScenarioVideoInfo(video.getVid());
		if (videoInfo != null) {
			Intent intent = new Intent();
			intent.setClass(mContext, PaikeVideoDetailActivity2.class);
			intent.putExtra("videoInfo", videoInfo);
			mContext.startActivity(intent);
		}
	}

	/**
	 * 跳转网页
	 * 
	 * @param title
	 * @param url
	 */
	private void transferWebView(String title, String url) {
		Intent intent = new Intent();
		intent.setClass(mContext, WebViewActivity3.class);
		intent.putExtra("adLink", url);
		intent.putExtra(WebViewActivity3.FLAG_TITLE, title);
		mContext.startActivity(intent);
	}

	/**
	 * 跳转VR
	 * 
	 * @param url
	 */
	private void transferVR(String url) {
		Intent intent = new Intent();
		intent.setClass(mContext, VideoPlayerVRActivity.class);
		intent.putExtra("videourl", url);
		mContext.startActivity(intent);
	}

	/**
	 * 跳转主题详情
	 * 
	 * @param title
	 * @param tid
	 * @param imgUrl
	 */
	private void transferThemeDetail(String title, String tid, String imgUrl) {
		Intent intent = new Intent();
		intent.setClass(mContext, ThemeDetailActivity.class);
		intent.putExtra("title", title);
		intent.putExtra("themdId", tid);
		intent.putExtra("themeImg", imgUrl);
		mContext.startActivity(intent);
	}

	private class Holder {
		public ImageView userimg;
		public ImageView ratingimg;
		public TextView name;
		public TextView time;
		public TextView describe;
		public ImageView image;
		public TextView title;
	}

}
